;; =============================================================================
;; utility functions

(in-package :oali.core)

;; transforms given symbol to a C identifier string
(defun symbol-to-c-id (sym)
  ;; simple approach: print to string id as it is
  ;; TODO: distinguish non-C characters
  (format nil "~a" sym))

;; get type associated with the given id
;; throws error if there is no such an id
(defun existing-type (context type-id)
  (let ((types (types context))
	result)
    (setf result (gethash type-id types))
    (if result result (error
		       (format nil "type `~a' is not declared before"
			       type-id)))))

;; adds new type, throw error if such a type has been already declared
(defun add-new-type (context type-id type-inst)
  (let ((types (types context)))
    (if (gethash type-id types) (error
				 (format nil "type `~a' already declared"
					 type-id)))
    (setf (gethash type-id types) type-inst)))

;; helper function for signed/unsigned type definition
(defun add-new-qualified-type (context qualifier args)
  (destructuring-bind (existing-type-id new-type-id) args
    (add-new-type context new-type-id
		  (make-instance 'p-qualified-type
				 :qualifier qualifier
				 :inferior-type (existing-type
						 context
						 existing-type-id)))))

